package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.feign.AlipayFeignApi;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private AlipayFeignApi alipayFeignApi;

    @RequestMapping("alipay")
    public Result<String> alipay(String orderNo, int type) {
        /*
        1、参数的校验
        2、根据type判断是在线支付，还是积分支付
        3、如果是在线支付启动远程调用支付服务进行支付操作
         */

        //参数的校验
        if (StringUtils.isEmpty(orderNo) || StringUtils.isEmpty(type + "")) {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }

        //根据type判断是在线支付，还是积分支付
        if (OrderInfo.PAYTYPE_ONLINE.equals(type)) {
            //走在线支付操作
            String result = orderInfoService.payOnline(orderNo, type);
            return Result.success(result);
        } else if (OrderInfo.PAYTYPE_INTERGRAL.equals(type)) {
            //走积分支付操作
            orderInfoService.payIntergral(orderNo,type);
        } else {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success("");
    }

    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;

    //同步回调数据，同时跳转到订单详情的界面
    @RequestMapping("/return_url")
    public void return_url(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        System.out.println("同步调用：" + new Date());
        //进行验签操作
        //远程支付进行验签操作
        Result<Boolean> result = alipayFeignApi.checkSign(params);
        //拿到订单号
        String orderNo = params.get("out_trade_no");
        if (result != null && !result.hasError()) {
            if (result.getData()) {
                //跳转到订单详情的界面
                response.sendRedirect(frontEndPayUrl + orderNo);
            } else {
                response.sendRedirect("http://localhost/50x.html");
            }
        }
    }

    @RequestMapping("/notify_url")
    public void notify_url(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        System.out.println("异步回调：" + new Date());
        //拿到订单号
        String orderNo = params.get("out_trade_no");
        PrintWriter out = response.getWriter();
        //远程支付进行验签操作
        Result<Boolean> result = alipayFeignApi.checkSign(params);
        if (result != null && !result.hasError()) {
            if (result.getData()) {
                orderInfoService.paySuccess(orderNo);
                out.write("success");
            } else {
                out.write("fail");
            }
        }
    }

    @RequestMapping("/refund")
    public Result<String> refund(String orderNo) {
        //参数校验
        if (StringUtils.isEmpty(orderNo)) {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        //根据订单编号将订单信息查询出来
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        //判断订单信息是线上支付，还是积分支付
        if (OrderInfo.PAYTYPE_ONLINE.equals(orderInfo.getPayType())) {
            //线上支付退款流程
            orderInfoService.refund(orderInfo);
        } else if (OrderInfo.PAYTYPE_INTERGRAL.equals(orderInfo.getPayType())) {
            //积分支付退款流程
            orderInfoService.refundIntergral(orderInfo);
        } else {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success("");//刷新页面
    }
}
